import {
    Login
} from "../modeal/login.js"
import {
    promisic
} from "./util.js";
import {
    config
} from "../config/config";


class Http {

    static async request({
        url,
        data = {},
        method = "POST"
    }) {
        let token = wx.getStorageSync("token")
        data.token = token
        try {
            const res = await promisic(wx.request)({
                url: `${config.apiBaseUrl}${url}`,
                data,
                method,
                header: {
                    // appkey: config.appkey
                }
            });
            let statusCode = res.statusCode.toString()

            let status = res.data.status
            // let errorCode = res.data.error_code
            console.log(res, "http")

            if (statusCode.startsWith('2')) {
                if (status == 0) {
                    return res.data
                }
                // 301 token失效
                if (status == 301) {
                    // 重新获取token
                    Login.wxLogin()
                    return
                }
                this.showModal(res.data.msg)
                return
            }

            console.log(res, "ereror")
            this.showModal(res.data.data.msg)

            // return res.data;
            // if (code.startsWith('2')) {
            //     return res.data;
            // } else {

            //     console.log(res, "eror")
            // }

        } catch (err) {
            console.log(err)
            this.showModal('出现了错误')
        }

    }

    static showModal(title) {
        wx.showToast({
            title: title,
            icon: 'none',
            duration: 2000
        })
    }
}

export {
    Http
};